<?php

class LocationLogic {

  function save(&$location)
  {
  	$locationDao = new LocationDAO();

  	return $locationDao->save($location) == 1;
  }

  function load($id)
  {
  	$locationDao = new LocationDAO();
  	return $locationDao->get($id);
  }


  function findAll()
  {
    $locationDao = new LocationDAO();
    return $locationDao->findWhere("", "name");
  }

  function findByName ($name)
  {
  	$locationDao = new LocationDAO();
  	return $locationDao->findWhere("name like '%$name%'");
  }

   function findByExample ($location, $address = null)
  {
  	 $locationDao = new LocationDAO();
  	 if ($address != null)
  	 {
  	 	$addressDao = new AddressDAO();
  	 	$addresses = $addressDao->findByExample($address);

  	 	$all = array();

  	 	foreach ($addresses as $addresult)
  	 	{
  	 		$location->address_id = $addresult->id;
  	 		foreach ($locationDao->findByExample($location) as $result)
  	 		{
  	 			array_push($all, $result);
  	 		}
  	 	}

  	 	return $all;
  	 }
  	 else
  	 {
  	 	return $locationDao->findByExample($location);
  	 }
  }
}

?>
